
/* the following includes should be present */

#include <stdio.h>
#include <inttypes.h>
#include <wchar.h>

#include <float.h>   /* floating point machine constants */

int main(void)
{
  printf("---[ansi types]---\n");

  /* standard integral types */
  printf("sizeof(char)        = %d\n", sizeof(char));
  printf("sizeof(wchar_t)     = %d\n", sizeof(wchar_t));
  printf("sizeof(short)       = %d\n", sizeof(short));
  printf("sizeof(int)         = %d\n", sizeof(int));
  printf("sizeof(long)        = %d\n", sizeof(long));
  /* ISO C90 does not support 'long long' */

  /* pointers */

  printf("sizeof(void*)       = %d\n", sizeof(void*));
  printf("sizeof(intptr_t)    = %d\n", sizeof(intptr_t));

  /* floating point */

  printf("sizeof(float)       = %d\n", sizeof(float));
  printf("sizeof(double)      = %d\n", sizeof(double));
  printf("sizeof(long double) = %d\n", sizeof(long double));

  printf("---[inttypes.h types]---\n");
  
  printf("sizeof(int8_t)      = %d\n", sizeof(int8_t));
  printf("sizeof(int16_t)     = %d\n", sizeof(int16_t));
  printf("sizeof(int32_t)     = %d\n", sizeof(int32_t));
  printf("sizeof(int64_t)     = %d\n", sizeof(int64_t));


  /* register and pointer size printing */


  printf("\n");

  switch (sizeof(int))
    {
    case sizeof(int8_t):  printf("8-bit registers, ");break;
    case sizeof(int16_t): printf("16-bit registers, ");break;
    case sizeof(int32_t): printf("32-bit registers, ");break;
    case sizeof(int64_t): printf("64-bit registers, ");break;
    default: printf("unknown machine register size\n");
    }

  switch (sizeof(void*))
    {
    case sizeof(int8_t):  printf("8-bit pointers.\n");break;
    case sizeof(int16_t): printf("16-bit pointers.\n");break;
    case sizeof(int32_t): printf("32-bit pointers.\n");break;
    case sizeof(int64_t): printf("64-bit pointers.\n");break;
    default: printf("unknown machine register size\n");
    }

  /* determine endianess */
  {
    union my_union {
      char mem[sizeof(int)];
      int i;
    };
    union my_union u;

    u.i = 1;
    if (u.mem[0] == 1) printf("little-endian machine\n");
    else if (u.mem[sizeof(int)-1] == 1) printf("big-endian machine\n");
    else printf("oops. cannot determine big or little endian:( is this a PDP-11 or what?\n");
  }


  /* floating point arithmetic */

  printf("\n---[floating point machine constants]---\n");

  return 0;
}

